package ylsb.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ylsb.model.*;
import ylsb.service.YltpXiangmuService;
import ylsb.service.YltprenyuanfenpeiService;
import ylsb.service.YltpyinjinService;
import ylsb.service.YltpyuanzeService;
import ylsb.support.JsonResult;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("yuanze")
public class YltpYuanzeController {
    @Resource
    YltpyuanzeService ddService;
    @Resource
    YltpXiangmuService ddYltpXiangmuService;
    @Resource
    YltpyinjinService ddYltpyinjinService;
    @Resource
    YltprenyuanfenpeiService ddYltprenyuanfenpeiService;

    public Logger log = LoggerFactory.getLogger(YltpBaofeiController.class);

    @RequestMapping("find_one")
    public JsonResult<?> findOne(
            @RequestParam("detailid") String ppdetailid,
            @RequestParam("xiangmuid") String ppxiangmuid,
            @RequestParam("userid") String ppuserid
    ){
        try {
            return JsonResult.getSuccessResult(ddService.findOne(ppdetailid,ppxiangmuid,ppuserid));
        }catch(Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("查询失败！"+e);
        }
    }

    @RequestMapping("save")
    public synchronized JsonResult<?> save( @RequestParam("detailid") String ppdetailId,
                                            @RequestParam("xiangmuid") String ppxiangmuid,
                                            @RequestParam("shiyongyuanzeid") String ppshiyongyuanzeid,
                                            @RequestParam("userid") String ppuserId){

        try {
            YltpYuanze mmyuanze=new YltpYuanze();
            YltpYinjin mmYinjin=ddYltpyinjinService.findOne(ppdetailId);

            YltpXiangmu mmxiangmu=ddYltpXiangmuService.findOne(mmYinjin.getXiangmuid());
            if(mmxiangmu.getIsactived()!=1){
                return JsonResult.getErrorResult("保存失败，投票尚未启动");
            }

            List<YltpRenyuanfenpei> listfenpei = ddYltprenyuanfenpeiService.findbyUserIDandXiangmuId(ppuserId,mmYinjin.getXiangmuid());
            if(listfenpei.get(0).getIssubmit()==1){
                return JsonResult.getErrorResult("保存失败，当前投票项已提交");
            }

            List<YltpYuanze> mmshifoucunzai=ddService.findOne(ppdetailId,ppxiangmuid,ppuserId);
            if(mmshifoucunzai.size()>0){
                mmyuanze.setId(mmshifoucunzai.get(0).getId());
                mmyuanze.setShiyongyuanzeid(ppshiyongyuanzeid);
                return ddService.modify(mmyuanze)==true? JsonResult.getSuccessResult("保存成功")
                        : JsonResult.getErrorResult("保存失败");
            }else{
                mmyuanze.setId(UUID.randomUUID().toString());
                mmyuanze.setDetailid(ppdetailId);
                mmyuanze.setUserid(ppuserId);
                mmyuanze.setXiangmuid(ppxiangmuid);
                mmyuanze.setShiyongyuanzeid(ppshiyongyuanzeid);
                mmyuanze.setCreatetime(new Date());

                return ddService.add(mmyuanze)==true? JsonResult.getSuccessResult("保存成功")
                        : JsonResult.getErrorResult("保存失败");
            }

        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("yiyongyuanze/save:error " + e.getMessage());
        }
    }
}
